package com.atguigu.yygh.orders.service.impl;


import com.atguigu.yygh.enums.RefundStatusEnum;
import com.atguigu.yygh.model.order.PaymentInfo;
import com.atguigu.yygh.model.order.RefundInfo;
import com.atguigu.yygh.orders.mapper.RefundInfoMapper;
import com.atguigu.yygh.orders.service.RefundInfoService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * <p>
 * 退款信息表 服务实现类
 * </p>
 *
 * @author atguigu
 * @since 2021-11-16
 */
@Service
public class RefundInfoServiceImpl extends ServiceImpl<RefundInfoMapper, RefundInfo> implements RefundInfoService {

    @Override
    public RefundInfo saveRefundInfo(PaymentInfo paymentInfo) {

        //查看是否已经有退款信息
        RefundInfo refundInfo = baseMapper.selectOne(new QueryWrapper<RefundInfo>()
                .eq("order_id", paymentInfo.getOrderId()));

        if (null != refundInfo){
            return refundInfo;//已存在
        }

        // 保存交易记录
        refundInfo = new RefundInfo();
        refundInfo.setCreateTime(new Date());
        refundInfo.setOrderId(paymentInfo.getOrderId());
        refundInfo.setPaymentType(paymentInfo.getPaymentType());
        refundInfo.setOutTradeNo(paymentInfo.getOutTradeNo());
        refundInfo.setRefundStatus(RefundStatusEnum.UNREFUND.getStatus());//设置状态 --退款中
        refundInfo.setSubject(paymentInfo.getSubject());


        //新增退款信息
        baseMapper.insert(refundInfo);
        return refundInfo;
    }
}
